package com.focus.xyz.union.cases.frontend;

import com.focus.xyz.core.common.annotation.Task;
import com.focus.xyz.core.common.webdriver.DriverFactory;
import com.focus.xyz.union.action.KeyWordAction;
import com.focus.xyz.core.common.listeners.TC;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.core.utils.ExcelUtils;
import com.focus.xyz.core.utils.ProcessUtil;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import com.focus.xyz.union.pages.frontend.AcctRechargePage;
import com.focus.xyz.union.pages.frontend.FrontMenuList;
import com.focus.xyz.union.pages.frontend.UnionPayPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.*;

/**
 * Created by chenmeng on 2016/5/25.
 */
public class AcctRechargeTest extends TC {
    private static transient Logger logger = LoggerFactory.getLogger(AcctRechargeTest.class);

    private WebDriver driver;
    private KeyWordAction action;
    private ExcelUtils eu;

    @BeforeClass
    public void beforeClass() throws Exception{
        eu = new ExcelUtils(ConfigPropInfo.TEST_DATA_DIR + ConfigPropInfo.TEST_UNION_DATA_FILE);
        action.DelUnionFrontCookie();
    }

    @Parameters({"nodeUrl","browser","userName","password"})
    @BeforeMethod(alwaysRun = true)
    public void setUp(String nodeUrl,String browser,String userName,String password) throws Exception {
        driver = DriverFactory.getDriver(nodeUrl).getBrowser(browser);
        super.init(driver);
        action = new KeyWordAction(driver);
        action.LoginFront(userName,password);
    }


    @Parameters({"browser"})
    @AfterMethod(alwaysRun = true)
    public void tearDown(String browser) {
        try{
//            super.quit();
            driver.quit();
        }catch (Exception e){
            ProcessUtil.closeBrowser(browser);
        }finally {

        }
    }

    @Parameters({"amount"})
    @Test(description = "账户充值流程校验")
    public void ACCT_RECHARGE(String amount) throws Exception{
        FrontMenuList fm = new FrontMenuList(driver,action);
        fm.openMenu(Constants.U_F_ACCT_MENU);
        action.pause(1000l);
        AcctRechargePage ap = new AcctRechargePage(driver,action);
        ap.clickRechargeBtn();
        action.switchToWindowByUrl("http://union.xyz.cn/accrecharge/toRecharge",true);
        action.waitForPageLoad();
        ap.rechargeAcct(amount);
//        action.switchToWindowByUrl("http://payment-test.chinapay.com/pay/SnetPin.jsp",true);
        UnionPayPage up = new UnionPayPage(driver,action);
        action.pause(1000l);
        up.switchTo();
        up.unionpay();
    }


    @Parameters({"amount"})
    @Test(description = "账户充值后校验账户余额是否正确")
    public void ACCT_AMOUNT_CHECK(String amount) throws Exception{
        FrontMenuList fm = new FrontMenuList(driver,action);
        fm.openMenu(Constants.U_F_ACCT_MENU);
        action.pause(1000l);
        AcctRechargePage ap = new AcctRechargePage(driver,action);
        logger.info(amount + "-----" +ap.getAmount());
        if(!amount.equals(ap.getAmount())){
            action.fail("账户余额与充值金额不符");
        }

    }


    @Task(taskName = "UPDATE_ACCT_STATUS.sql",paras = {"param0=cimon009"})
    @Parameters({"payPass"})
    @Test(description = "激活充值账户校验")
    public void ACTIVATE_ACCT(String payPass) throws Exception{
        FrontMenuList fm = new FrontMenuList(driver,action);
        fm.openMenu(Constants.U_F_ACCT_MENU);
        action.pause(1000l);
        AcctRechargePage ap = new AcctRechargePage(driver,action);
        if(action.isElementExsit(By.xpath("//a[contains(text(),'立即验证')]"))){
            ap.verifyMobile("18811112222","1234");
            action.pause(1000l);
            action.assertStringTrue("恭喜您，手机号码验证成功");
        }else{
            action.fail("充值账户未激活不应该进入");
        }
        fm.openMenu(Constants.U_F_ACCT_MENU);
        ap.setPayPass(payPass);
        action.pause(1000l);
        action.assertStringTrue("账户余额");
    }

    @Test(description = "当充值账户余额不足时，同时使用账户余额与银联账户支付")
    public void ACCT_AND_CHINAUNION_PAY() throws Exception{
        /*
        * TODO:校验充值账户余额不足时是否可以支付成功。
        * */


    }

}
